﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BLO
{
    /// <summary>
    /// User who use BlogsProvider.
    /// </summary>
    public class User
    {
        DTO.User info;
        DAO.User userDAO;

        public User()
        {
            this.userDAO = new DAO.User();
            this.info = new DTO.User();
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="ID">UserID.</param>
        public User(String ID)
        {
            this.userDAO = new DAO.User();
            this.info = new DTO.User(ID);
        }
        public User(String Id, String pass)
        {
            this.userDAO = new DAO.User();
            this.info = userDAO.GetUser(Id, pass);
        }

        /// <summary>
        /// User Information.
        /// </summary>
        public DTO.User Info
        {
            get { return this.info; }
            set { this.info = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        public static User GetUser(String ID)
        {
            DAO.User userDAO = new DAO.User();
            User user = new User();
            user.Info = userDAO.GetUser(ID);
            if (user.Info == null)
            {
                Util.Log.LogError("Can not get UserInfo form DB.");
                user.info = new DTO.User();
            }

            return user;
        }

        /// <summary>
        /// Insert user information to database.
        /// </summary>
        public int Insert()
        {
           return userDAO.InsertUser(this.info);
        }

        /// <summary>
        /// Update information to database.
        /// </summary>
        public void Update()
        {
            userDAO.UpdateUser(this.info);
        }
        public int updateBlogInfo()
        {
            return userDAO.UpdateBlogInfo(this.info);
        }
        /// <summary>
        /// Re-get all information from database about this users.
        /// </summary>
        public void Refresh()
        {
            this.info = userDAO.GetUser(this.info.ID);
        }

        /// <summary>
        /// Delete this news from database.
        /// </summary>
        public void Delete()
        {
            userDAO.DeleteUser(this.info);
        }

        /// <summary>
        /// Directory of user's owned folder.
        /// </summary>
        public String UserDirectory 
        {
            get 
            {
                return Util.ServerPath.UserData + this.info.ID + "/"; 
            }
        }

        /// <summary>
        /// User Name.
        /// If user name is null, return user id.
        /// </summary>
        public String Name
        {
            get 
            {
                if (this.info == null)
                    return "";
                if (this.info.Name == null || "".Equals(this.info.Name))
                {
                    return this.info.ID;
                }
                else
                {
                    return this.info.Name;
                }
            }
        }

        /// <summary>
        /// User's Avatar Image
        /// </summary>
        public String Avatar
        {
            get 
            {
                if (this.info.AvatarPath == null || "".Equals(this.info.AvatarPath))
                    return Util.ServerPath.UserData + "Default/Avatar/Avatar.png";
                else
                    return this.UserDirectory + "Avatar/" + this.info.AvatarPath;
            }
        }

        /// <summary>
        /// Head image for blog.
        /// </summary>
        public String CoverImage
        {
            get 
            {
                if (this.info.CoverImagePath == null || "".Equals(this.info.CoverImagePath))
                    return Util.ServerPath.UserData + "Default/HeadImage/HeadImage.jpg";
                else
                    return this.UserDirectory + "HeadImage/" + this.info.CoverImagePath;
            }
        }

        /// <summary>
        /// Blog's name.
        /// </summary>
        public String BlogName
        {
            get 
            {
                if (this.info.BlogName == null)
                    return this.Name;
                else
                    return this.info.BlogName; 
            }
        }

        /// <summary>
        /// Blog's description
        /// </summary>
        public String BlogDescription
        {
            get 
            {
                if (this.info.BlogDescription == null)
                    return "";
                else
                    return this.info.BlogDescription;
            }
        }

        /// <summary>
        /// File XML contain setting for user's module.
        /// </summary>
        public String ModuleSettingFile
        {
            get 
            {
                return this.UserDirectory + "Module.xml";
            }
        }
    }
}